<!doctype html>



  


<html class="theme-next pisces use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  
    
      
    

    
  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic|Monda:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Git," />








  <link rel="shortcut icon" type="image/x-icon" href="/images/favicon.png?v=5.0.1" />






<meta name="description" content="概述暂存区是Git中非常重要的一个概念，理解暂存区的概念，可以帮助你理解Git的很多操作到底干了什么">
<meta property="og:type" content="article">
<meta property="og:title" content="Git——工作区与暂存区">
<meta property="og:url" content="ooho.me/2016/07/29/Git——工作区与暂存区/index.html">
<meta property="og:site_name" content="Ooho's blog">
<meta property="og:description" content="概述暂存区是Git中非常重要的一个概念，理解暂存区的概念，可以帮助你理解Git的很多操作到底干了什么">
<meta property="og:image" content="ooho.me/2016/07/29/Git——工作区与暂存区/git_add.svg">
<meta property="og:image" content="ooho.me/2016/07/29/Git——工作区与暂存区/git_start.svg">
<meta property="og:image" content="ooho.me/2016/07/29/Git——工作区与暂存区/git_untrack.svg">
<meta property="og:image" content="ooho.me/2016/07/29/Git——工作区与暂存区/git_add.svg">
<meta property="og:image" content="ooho.me/2016/07/29/Git——工作区与暂存区/git_commit.svg">
<meta property="og:updated_time" content="2016-07-29T09:35:16.588Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Git——工作区与暂存区">
<meta name="twitter:description" content="概述暂存区是Git中非常重要的一个概念，理解暂存区的概念，可以帮助你理解Git的很多操作到底干了什么">
<meta name="twitter:image" content="ooho.me/2016/07/29/Git——工作区与暂存区/git_add.svg">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"right","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="ooho.me/2016/07/29/Git——工作区与暂存区/"/>

  <title> Git——工作区与暂存区 | Ooho's blog </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-right page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">Ooho's blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">Intergrated delay cancer</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-terminal"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-folder-open-o"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-clone"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-square-o"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Git——工作区与暂存区
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-07-29T10:37:40+08:00" content="2016-07-29">
              2016-07-29
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/Git/" itemprop="url" rel="index">
                    <span itemprop="name">Git</span>
                  </a>
                </span>

                
                

              
            </span>
          

          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h2><p>暂存区是Git中非常重要的一个概念，理解暂存区的概念，可以帮助你理解Git的很多操作到底干了什么</p>
<img src="/2016/07/29/Git——工作区与暂存区/git_add.svg" alt="git_add.svg" title="">
<a id="more"></a>
<h2 id="工作区"><a href="#工作区" class="headerlink" title="工作区"></a>工作区</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 执行命令, 初始化一个`git`仓库：`test`</span></div><div class="line">mkdir <span class="built_in">test</span> &amp;&amp; <span class="built_in">cd</span> <span class="built_in">test</span></div><div class="line">git init</div></pre></td></tr></table></figure>
<p><code>test</code>仓库的根目录就是<strong>工作区</strong> (Working directory)  </p>
<h2 id="版本库"><a href="#版本库" class="headerlink" title="版本库"></a>版本库</h2><p>在工作区中，可以找到一个隐藏文件夹<code>.git</code> —— 即<strong>版本库</strong>(Repository)  </p>

<p>在版本库中最重要的，就是<strong>暂存区</strong>stage和<strong>分支</strong>branch（本文暂不讨论暂存区与分支具体分别对应哪些文件/文件夹）</p>
<h3 id="暂存区-amp-分支"><a href="#暂存区-amp-分支" class="headerlink" title="暂存区 &amp; 分支"></a>暂存区 &amp; 分支</h3><p>将文件存入版本库分两个步骤:</p>
<ol>
<li><code>git add</code> —— 实际上就是将文件添加到<u>暂存区</u></li>
<li><code>git commit</code> —— 将暂存区所有内容提交到<u>当前分支</u></li>
</ol>
<blockquote>
<p>分支在之后的文章中详细讲解，本文中只使用默认分支：master</p>
</blockquote>
<h2 id="一个简单的例子"><a href="#一个简单的例子" class="headerlink" title="一个简单的例子"></a>一个简单的例子</h2><p>用一个例子，讲解<u>文件存入版本库的过程</u><br>从中了解工作区，暂存区，分支（这里只讨论master分支）是如何工作的</p>
<h3 id="初始状态"><a href="#初始状态" class="headerlink" title="初始状态"></a>初始状态</h3><p>工作区、暂存区stage、分支<code>master</code>都是空的</p>
<img src="/2016/07/29/Git——工作区与暂存区/git_start.svg" alt="git_start.svg" title="">
<h3 id="添加文件"><a href="#添加文件" class="headerlink" title="添加文件"></a>添加文件</h3><p>在工作区添加一些文件，此时所有的文件的状态都是<code>untracked</code></p>
<img src="/2016/07/29/Git——工作区与暂存区/git_untrack.svg" alt="git_untrack.svg" title="">  
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 使用git status查看状态</span></div><div class="line">$ git status</div><div class="line">On branch master</div><div class="line">Untracked files:</div><div class="line">  (use <span class="string">"git add &lt;file&gt;..."</span> to include <span class="keyword">in</span> what will be committed)</div><div class="line"></div><div class="line">        File.txt</div><div class="line">        README.md</div><div class="line"></div><div class="line">nothing added to commit but untracked files present (use <span class="string">"git add"</span> to track)</div></pre></td></tr></table></figure>
<h3 id="将文件添加到暂存区"><a href="#将文件添加到暂存区" class="headerlink" title="将文件添加到暂存区"></a>将文件添加到暂存区</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"># 执行命令`git add`</div><div class="line">$ git add -A</div></pre></td></tr></table></figure>
<p>文件将会添加到暂存区，此时所有的文件的状态都是<code>staged</code></p>
<img src="/2016/07/29/Git——工作区与暂存区/git_add.svg" alt="git_add.svg" title=""> 
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 使用git status查看状态</span></div><div class="line">$ git status</div><div class="line">On branch master</div><div class="line">Changes to be committed:</div><div class="line">  (use <span class="string">"git reset HEAD &lt;file&gt;..."</span> to unstage)</div><div class="line"></div><div class="line">        new file:   File.txt</div><div class="line">        new file:   README.md</div></pre></td></tr></table></figure>
<h3 id="将文件提交至当前分支master"><a href="#将文件提交至当前分支master" class="headerlink" title="将文件提交至当前分支master"></a>将文件提交至当前分支<code>master</code></h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 执行命令`git commit`</span></div><div class="line">$ git commit -m <span class="string">"first commit"</span></div><div class="line">[master 5d2e29a] first commit</div><div class="line"> 2 files changed, 0 insertions(+), 0 deletions(-)</div><div class="line"> create mode 100644 File.txt</div><div class="line"> create mode 100644 README.md</div></pre></td></tr></table></figure>
<p>文件将会提交至当前分支<code>master</code> </p>
<img src="/2016/07/29/Git——工作区与暂存区/git_commit.svg" alt="git_commit.svg" title="">  
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 使用git status查看状态</span></div><div class="line">$ git status</div><div class="line">On branch master</div><div class="line">nothing to commit, working directory clean</div></pre></td></tr></table></figure>
<h2 id="参考-推荐的资源"><a href="#参考-推荐的资源" class="headerlink" title="参考/推荐的资源"></a>参考/推荐的资源</h2><ul>
<li><a href="http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013745374151782eb658c5a5ca454eaa451661275886c6000" target="_blank" rel="external">Git教程——廖雪峰</a></li>
<li><a href="http://git.oschina.net/progit/" target="_blank" rel="external">Pro Git（中文版）</a></li>
<li><a href="http://gitref.org/zh/index.html" target="_blank" rel="external">Git参考手册</a></li>
</ul>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Git/" rel="tag">#Git</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/07/27/没有标题只有图片的文章/" rel="next" title="Images">
                <i class="fa fa-chevron-left"></i> Images
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.jpg"
               alt="Ooho" />
          <p class="site-author-name" itemprop="name">Ooho</p>
          <p class="site-description motion-element" itemprop="description">Intergrated delay cancer</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">3</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">4</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#概述"><span class="nav-number">1.</span> <span class="nav-text">概述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#工作区"><span class="nav-number">2.</span> <span class="nav-text">工作区</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#版本库"><span class="nav-number">3.</span> <span class="nav-text">版本库</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#暂存区-amp-分支"><span class="nav-number">3.1.</span> <span class="nav-text">暂存区 & 分支</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#一个简单的例子"><span class="nav-number">4.</span> <span class="nav-text">一个简单的例子</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#初始状态"><span class="nav-number">4.1.</span> <span class="nav-text">初始状态</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#添加文件"><span class="nav-number">4.2.</span> <span class="nav-text">添加文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#将文件添加到暂存区"><span class="nav-number">4.3.</span> <span class="nav-text">将文件添加到暂存区</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#将文件提交至当前分支master"><span class="nav-number">4.4.</span> <span class="nav-text">将文件提交至当前分支master</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#参考-推荐的资源"><span class="nav-number">5.</span> <span class="nav-text">参考/推荐的资源</span></a></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Ooho</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="http://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.0.1"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  



  




  
  

  

  

  

</body>
</html>
